import requests

class station(object):

    def __init__(self):
        self.url='https://kyfw.12306.cn/otn/resources/js/framework/station_name.js'

      # *Function:  Unicode2HexStr  
      
      # * Description：  Unicode字符串转换为16进制字符串  
      
      # *Input:  Unicde_Str要转换的Unicode字符串  
      
      # *Return:  Hex_Str返回16进制字符串  
      
      # *Others:  

    def Unicode2HexStr(self,Unicde_Str):

        Hex_Str = ''  

        for i in range(0, len(Unicde_Str)):  

            Hex_Str += (hex(ord(Unicde_Str[i])).upper().replace('0X','%u').zfill(4))  

        return Hex_Str  

    def station_dict(self):#从12306获取车站信息，构建车站cookie代码字典
        r = requests.get(self.url)
        text=(r.text)[21:-2]
        slist=text.split('@')
        dic={}
        for s in slist:
            sl=s.split('|')
            name_cn=sl[1]
            cod=sl[2]
            dic[name_cn]=self.Unicode2HexStr(name_cn)+'%2C'+cod
        return dic


    def get_station_code(self,name):
        return self.station_dict()[name]


